<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class LMProjectInfo
{
    protected $project;
    protected $matches_played = -1;

    public function __construct(LM_Table_Project $project)
    {
        $this->project = $project;
    }

    public function getName()
    {
        return $this->project->getName();
    }

    public function getTeams()
    {
        return $this->project->getTeams();
    }

    public function getClubs()
    {
        $database = $this->project->getDB();
        $database->setQuery(
                "SELECT c.*, tm.teammap_id
				   FROM jos_leagueman_club AS c
			 INNER JOIN jos_leagueman_team AS t ON t.team_club = c.club_id
			 INNER JOIN jos_leagueman_teammap AS tm ON tm.teammap_team = t.team_id AND tm.teammap_project = " . $this->project->getID() . "
			   ORDER BY c.club_name"
        );

        return $database->loadObjectlist();
    }

    public function getRounds()
    {
        return $this->project->getRounds();
    }

    public function getHighestHomeWins()
    {
        $database = $this->project->getDB();
        $database->setQuery(
                "SELECT m.*, t1.team_name AS team1, t2.team_name AS team2, (m.match_result_0_1 - m.match_result_0_2) AS diff
			   FROM jos_leagueman_match m
		 INNER JOIN jos_leagueman_team t1 ON t1.team_id = m.match_team1
		 INNER JOIN jos_leagueman_team t2 ON t2.team_id = m.match_team2
			  WHERE match_project = " . $this->project->getID() . "
		   ORDER BY diff DESC LIMIT 3"
        );
        return $database->loadObjectList();
    }

    public function getHighestAwayWins()
    {
        $database = $this->project->getDB();
        $database->setQuery(
                "SELECT m.*, t1.team_name AS team1, t2.team_name AS team2, (m.match_result_0_2 - m.match_result_0_1) AS diff
			   FROM jos_leagueman_match m
		 INNER JOIN jos_leagueman_team t1 ON t1.team_id = m.match_team1
		 INNER JOIN jos_leagueman_team t2 ON t2.team_id = m.match_team2
			  WHERE match_project = " . $this->project->getID() . "
		   ORDER BY diff DESC LIMIT 3"
        );
        return $database->loadObjectList();
    }

    public function getTotalMatches()
    {
        $database = $this->project->getDB();
        $database->setQuery(
                "SELECT COUNT(match_id)
			   FROM jos_leagueman_match
			  WHERE match_project = " . $this->project->getID()
        );
        return $database->loadResult();
    }

    public function getCountMatchesPlayed()
    {
        if ($this->matches_played == -1) {

            $database = $this->project->getDB();
            $database->setQuery(
                    "SELECT COUNT(match_id)
			   	   FROM jos_leagueman_match
			  	  WHERE match_project = " . $this->project->getID() . "
			    	AND match_result_0_1 is not NULL
			    	AND match_result_0_2 is not NULL"
            );
            $this->matches_played = $database->loadResult();
        }

        return $this->matches_played;
    }

    public function getTotalGoals()
    {
        $database = $this->project->getDB();
        $database->setQuery(
                "SELECT SUM(match_result_0_1) + SUM(match_result_0_2) AS total
			   FROM jos_leagueman_match
			  WHERE match_project = " . $this->project->getID()
        );
        return $database->loadResult();
    }

    public function getTotalGoalsPerRound()
    {
        return round($this->getTotalGoals() / $this->project->getMaxRound(), 2);
    }

    public function getTotalGoalsPerMatch()
    {
        if ($this->matches_played > 0) {
            return round($this->getTotalGoals() / $this->getCountMatchesPlayed());
        } else {
            return 0;
        }
    }

    public function getTotalHomeGoals()
    {
        $database = $this->project->getDB();
        $database->setQuery(
                "SELECT SUM(match_result_0_1) AS total
			   FROM jos_leagueman_match
			  WHERE match_project = " . $this->project->getID()
        );

        return $database->loadResult();
    }

    public function getTotalHomeGoalsPerRound()
    {
        return round($this->getTotalHomeGoals() / $this->project->getMaxRound(), 2);
    }

    public function getTotalHomeGoalsPerMatch()
    {
        if ($this->matches_played > 0) {
            return round($this->getTotalHomeGoals() / $this->getCountMatchesPlayed());
        } else {
            return 0;
        }
    }

    public function getTotalAwayGoals()
    {
        $database = $this->project->getDB();
        $database->setQuery(
                "SELECT SUM(match_result_0_2) AS total
			   FROM jos_leagueman_match
			  WHERE match_project = " . $this->project->getID()
        );
        return $database->loadResult();
    }

    public function getTotalAwayGoalsPerRound()
    {
        return round($this->getTotalAwayGoals() / $this->project->getMaxRound(), 2);
    }

    public function getTotalAwayGoalsPerMatch()
    {
        if ($this->matches_played > 0) {
            return round($this->getTotalAwayGoals() / $this->getCountMatchesPlayed());
        } else {
            return 0;
        }
    }

    public function getMinRound()
    {
        return $this->project->getMinRound();
    }

    public function getMaxRound()
    {
        return $this->project->getMaxRound();
    }

    public function getCurrentRound()
    {
        return $this->project->getCurrentRound();
    }

    public function getLatestForumPosts($limit)
    {
        $category = $this->project->getForumCategory();
        if (!$category)
            return array();

        $database = $this->project->getDB();

        $where = array();
        $where[] = "b.parent = 0";
        $where[] = "a.moved != 1";
        $where[] = "a.catid = " . $category;

        $database->setQuery(
                "SELECT a.thread, b.subject
			   FROM #__sb_messages AS a 
		  LEFT JOIN #__sb_messages AS b ON a.thread = b.thread
		  	  WHERE " . implode("\n AND ", $where) . "
		   GROUP BY a.thread 
		   ORDER BY a.time DESC
			  LIMIT $limit");

        return $database->loadObjectList();
    }

    public function getForumItemid()
    {
        $database = $this->project->getDB();

        $database->setQuery("SELECT id FROM jos_menu WHERE link = 'index.php?option=com_jbulletin'");
        return $database->loadResult();
    }

}